package com.ytjj.bigdata.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.bigdata.dao.AdsProductCountMapper;
import com.ytjj.bigdata.entity.AdsProductCount;
import com.ytjj.bigdata.model.dto.AdsProductCountDto;
import com.ytjj.bigdata.model.response.AdsProductResponse;
import com.ytjj.common.api.CommonPageExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
@Slf4j
public class AdsProductCountService {
    @Resource
    private AdsProductCountMapper adsProductCountMapper;

    public CommonPageExtend selectAdsProduct(AdsProductCountDto adsProductCountDto) {
//        AdsProductResponse response = new AdsProductResponse();
        PageHelper.startPage(adsProductCountDto.getPageNum(), adsProductCountDto.getPageSize());
        List<AdsProductCount> list = adsProductCountMapper.selectAdsProduct(adsProductCountDto);

        if (CollectionUtils.isEmpty(list)) {
            return new CommonPageExtend();
        }

        List<AdsProductResponse> productCount = adsProductCountMapper.findAdsProductCount(adsProductCountDto);
        CommonPageExtend pageInfo = CommonPageExtend.restPage(list);

        AdsProductResponse response = productCount.get(0);
        pageInfo.setProductIdCount(response.getProductIdCount());
        pageInfo.setCostPriceCount(new BigDecimal(response.getCostPriceCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setPriceCount(new BigDecimal(response.getPriceCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setUvCount(response.getUvCount());
        pageInfo.setIntentionOrderCount(response.getIntentionOrderCount());
        pageInfo.setOrderCountCount(response.getOrderCountCount());
        pageInfo.setPayCountCount(response.getPayCountCount());
        pageInfo.setUnpayCountCount(response.getUnpayCountCount());
        // 订单率 -- order_count * 100/uv
        if (response.getUvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getOrderCountCount() * 100);
            BigDecimal b2 = new BigDecimal(response.getUvCount());
            pageInfo.setOrderRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
//        pageInfo.setOrderRateCount(new BigDecimal(response.getOrderCountCount() * 100 / response.getUvCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
        // 支付率 -- pay_count*100/order_count
        if (response.getOrderCountCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getPayCountCount() * 100);
            BigDecimal b2 = new BigDecimal(response.getOrderCountCount());
            pageInfo.setPayRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
//        pageInfo.setPayRateCount(new BigDecimal(response.getPayCountCount() * 100 / response.getOrderCountCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
//        pageInfo.setOrderRateCount(new BigDecimal(response.getOrderRateCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
//        pageInfo.setPayRateCount(new BigDecimal(response.getPayRateCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());

        return pageInfo;

        // order_count * 100/uv,pay_count*100/order_count

//        productCount.stream().forEach(item -> {
//            double costPriceCount = new BigDecimal(null !=  item.getCostPriceCount() ? item.getCostPriceCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double priceCount = new BigDecimal(null != item.getPriceCount() ? item.getPriceCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double orderRateCount = new BigDecimal(null != item.getOrderRateCount() ? item.getOrderRateCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double payRateCount = new BigDecimal(null != item.getPayRateCount() ? item.getPayRateCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            item.setCostPriceCount(costPriceCount);
//            item.setPriceCount(priceCount);
//            item.setOrderRateCount(orderRateCount);
//            item.setPayRateCount(payRateCount);
//        });
//
//        AdsProductVo adsProductResp = new AdsProductVo();
//        adsProductResp.setAdsProductCountList(list);
//        adsProductResp.setProductCount(productCount);

//        response.setProductIdCount(list.stream().filter(item -> null != item.getProductId()).mapToInt(AdsProductCount::getProductId).sum());
//        response.setCostPriceCount(list.stream().filter(item -> null != item.getCostPrice()).mapToDouble(AdsProductCount::getCostPrice).sum());
//        response.setPriceCount(list.stream().filter(item -> null != item.getPrice()).mapToDouble(AdsProductCount::getCostPrice).sum());
//        response.setUvCount(list.stream().filter(item -> null != item.getUv()).mapToInt(AdsProductCount::getUv).sum());
//        response.setIntentionOrderCount(list.stream().filter(item -> null != item.getIntentionOrder()).mapToInt(AdsProductCount::getIntentionOrder).sum());
//        response.setOrderCountCount(list.stream().filter(item -> null != item.getOrderCount()).mapToInt(AdsProductCount::getOrderCount).sum());
//        response.setPayCountCount(list.stream().filter(item -> null != item.getPayCount()).mapToInt(AdsProductCount::getPayCount).sum());
//        response.setUnpayCountCount(list.stream().filter(item -> null != item.getUnpayCount()).mapToInt(AdsProductCount::getUnpayCount).sum());
//        response.setOrderRateCount(list.stream().filter(item -> null != item.getOrderRate()).mapToDouble(AdsProductCount::getOrderRate).sum());
//        response.setPayRateCount(list.stream().filter(item -> null != item.getPayRate()).mapToDouble(AdsProductCount::getPayRate).sum());
//        response.setOrderRateCount(list.stream().filter(item -> StringUtils.isNotBlank(item.getOrderRate())).mapToDouble(d -> Double.parseDouble(d.getOrderRate())).sum());
//        response.setPayRateCount(list.stream().filter(item -> StringUtils.isNotBlank(item.getPayRate())).mapToDouble(d -> Double.parseDouble(d.getPayRate())).sum());

//        pageInfo.setProductIdCount(response.getProductIdCount());
//        pageInfo.setCostPriceCount(response.getCostPriceCount());
//        pageInfo.setPriceCount(response.getPriceCount());
//        pageInfo.setUvCount(response.getUvCount());
//        pageInfo.setIntentionOrderCount(response.getIntentionOrderCount());
//        pageInfo.setOrderCountCount(response.getOrderCountCount());
//        pageInfo.setPayCountCount(response.getPayCountCount());
//        pageInfo.setUnpayCountCount(response.getUnpayCountCount());
//        pageInfo.setOrderRateCount(response.getOrderRateCount());
//        pageInfo.setPayRateCount(response.getPayRateCount());


//        return adsProductResp;
    }
}
